package com.jxl.controller;

import com.jxl.pojo.GameInfo;
import com.jxl.pojo.Page;
import com.jxl.pojo.User;
import com.jxl.service.GameInfoService;
import com.jxl.web.WebResult;
import com.jxl.web.constant.Constant;
import com.jxl.web.constant.GameInfoConstant;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 游戏信息控制器
 * @author 江雪林
 * @create 2021-03-29
 */
@Controller
@RequestMapping("/gameinfo")
public class GameInfoController {
    @Resource
    private GameInfoService gameInfoService;

    @InitBinder
    public void dateBinder(WebDataBinder webDataBinder){
        webDataBinder.registerCustomEditor(Date.class,
                new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

    @RequestMapping(value = "/games/{pageNo}/{pageSize}",method = RequestMethod.GET)
    public ModelAndView getPassedGame(@PathVariable("pageNo")Integer pageNo,
                                      @PathVariable(value = "pageSize",required = false)Integer pageSize){
        if(pageNo <= 0){
            pageNo = 1;
        }
        if (pageSize<=0){
            pageSize = 5;
        }
        ModelAndView modelAndView = new ModelAndView("games");
        Page<GameInfo> pageOfPassedGame = gameInfoService.getPageOfPassedGame(pageNo, pageSize);
        modelAndView.addObject("page",pageOfPassedGame);
        return modelAndView;
    }

    @RequestMapping(value = "/ugames/{id}/{pageNo}/{pageSize}",method = RequestMethod.GET)
    public ModelAndView getGamesByUser(@PathVariable(value = "id")Integer id,
                                       @PathVariable("pageNo")Integer pageNo,
                                       @PathVariable(value = "pageSize",required = false)Integer pageSize){
        if(pageNo <= 0){
            pageNo = 1;
        }
        if (pageSize<=0){
            pageSize = 5;
        }
        ModelAndView modelAndView = new ModelAndView("usergames");
        Page<GameInfo> pageOfPassedGame = gameInfoService.getPageGamesByUserId(id,pageNo, pageSize);
        modelAndView.addObject("page",pageOfPassedGame);
        return modelAndView;
    }

    @RequestMapping(value = "/game/{id}",method = RequestMethod.GET)
    public ModelAndView getGameInfo(@PathVariable(value = "id")Integer id){
        if(id <= 0){
            return null;
        }
        ModelAndView modelAndView = new ModelAndView("update");
        modelAndView.addObject("game",gameInfoService.getGameInfoById(id));
        return modelAndView;
    }

    @RequestMapping(value = "/game/update",method = RequestMethod.POST)
    public ModelAndView update(GameInfo gameInfo, HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        if(gameInfo.getId()<=0 || gameInfo==null){
            return null;
        }
        gameInfo.setStatus(GameInfoConstant.WAIT_PASSED);
        if(gameInfoService.udpateGame(gameInfo)){
            modelAndView.setViewName("usergames");
            Page<GameInfo> pageOfPassedGame = gameInfoService.getPageGamesByUserId(((User)session.getAttribute(Constant.SESSION_USER)).getId(),1, 5);
            modelAndView.addObject("page",pageOfPassedGame);
        }else{
            modelAndView.setViewName("update");
            modelAndView.addObject("game",gameInfoService.getGameInfoById(gameInfo.getId()));
        }
        return modelAndView;
    }

    @RequestMapping(value = "/game/status/{id}",method = RequestMethod.GET)
    public ModelAndView updateStatus(@PathVariable("id")Integer id,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        if(id==null || id <=0){
            return null;
        }
        gameInfoService.udpateGame(new GameInfo(id, GameInfoConstant.PASSING));
        modelAndView.setViewName("usergames");
        Page<GameInfo> pageOfPassedGame = gameInfoService.getPageGamesByUserId(((User)session.getAttribute(Constant.SESSION_USER)).getId(),1, 5);
        modelAndView.addObject("page",pageOfPassedGame);
        return modelAndView;
    }
}
